package com.blb.dao.impl;


import com.blb.dao.UserDao;
import com.blb.pojo.User;
import com.blb.utils.DBUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.List;
import java.sql.SQLException;

@Repository
public class UserDaoImpl implements UserDao {
    /**
     * 根据id获取用户名称和头像
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public User getUserById(String id) throws Exception {
        Connection conn = DBUtils.getConn();
        User user = DBUtils.getOne(User.class, conn, "select username,image from s_user where id=? ", id);
        DBUtils.evict(conn);
        return user;
    }

    @Override
    public List<User> list() throws Exception {
        Connection conn = DBUtils.getConn();
        List<User> list = DBUtils.getList(User.class, conn, "select * from s_user ");
        DBUtils.evict(conn);
        return list;
    }
    @Override
    public User getByUsername(String username) throws Exception{
        Connection conn = DBUtils.getConn();
        User user = DBUtils.getOne(User.class, conn, "select * from s_user where username = ?", username);
        DBUtils.evict(conn);
        return user;
    }

    @Override
    public void add(User user) throws SQLException {
        Connection conn = DBUtils.getConn();
        DBUtils.executeUpdate(conn, "insert into s_user(id, username, password, image) values(?,?,?,?)", user.getId(), user.getUsername(), user.getPassword(), user.getImage());
        DBUtils.evict(conn);
    }
}
